package com.google.code.mochaccino.framework.ui.components.table;

/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Column {

	/** The column id */
	private String id;

	/** The title of the column */
	private String label;

	/** The sort direction */
	private String sortDirection;

	/** Resizable */
	private boolean resizable = true;

	/** Show Header? */
	private boolean showHeader = true;

	/** Sortable? */
	private boolean sortable = false;

	/** Is Column Visible */
	private boolean visible = true;

	/** Max Size In Px */
	private int maxSizeInPx = 1024;

	/** Size in Px */
	private int sizeInPx = 100;

	public static final String SORT_ASC  = "asc";
	public static final String SORT_DESC = "desc";

	/** Constructor */
	public Column( String columnId, final String content, boolean visible ) {
		this( columnId, content );
		this.visible = visible;
	}

	/** Constructor */
	public Column( String columnId, final String content, int sizeInPx, boolean visible ) {
		this( columnId, content );
		this.sizeInPx = sizeInPx;
		this.visible = visible;
	}

	/** Constructor with some component content */
	public Column( String columnId, String content ) {
		super();
		this.id = columnId;
		this.label = content != null ? content : "";
		if ( id == null ) {
			throw new IllegalArgumentException( "Id cannot be null!" );
		}
	}

	public Cell createCell( String content ) {
		Cell cell = new Cell( id, content );
		cell.setVisible( visible );
		cell.setSizeInPx( sizeInPx );
		return cell;
	}

	@Override
	public boolean equals( Object o ) {
		if ( o instanceof Column ) {
			Column opt = (Column) o;
			if ( id.equals( opt.id ) ) {
				return true;
			}
		}
		return super.equals( o );
	}

	/** @return the colId */
	public String getId() {
		return id;
	}

	/** @param colId the colId to set */
	public void setId( String colId ) {
		this.id = colId;
	}

	/** @return the content */
	public String getLabel() {
		return label;
	}

	/** @param content the content to set */
	protected void setLabel( String content ) {
		this.label = content;
	}

	public int getMaxSizeInPx() {
		return maxSizeInPx;
	}

	public void setMaxSizeInPx( int maxSizeInPx ) {
		this.maxSizeInPx = maxSizeInPx;
	}

	/** @return the sizeInPx */
	public int getSizeInPx() {
		return sizeInPx;
	}

	/** Set size In Px */
	void setSizeInPx( int sizeInPx ) {
		this.sizeInPx = sizeInPx;
	}

	/** Get Sort Direction */
	public String getSortDirection() {
		if ( sortable ) {
			return sortDirection;
		}
		return null;
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	public boolean isResizable() {
		return resizable;
	}

	public void setResizable( boolean resizable ) {
		this.resizable = resizable;
	}

	public boolean isShowHeader() {
		return showHeader;
	}

	public void setShowHeader( boolean showHeader ) {
		this.showHeader = showHeader;
	}

	public boolean isSortable() {
		return sortable;
	}

	public void setSortable( boolean sortable ) {
		this.sortable = sortable;
	}

	/** @return the visible */
	public boolean isVisible() {
		return visible;
	}

	/** @param visible the visible to set */
	public void setVisible( boolean visible ) {
		this.visible = visible;
	}

	/** Set sort direction */
	public void setSortDirection( String dir ) {
		if ( sortable ) {
			this.sortDirection = dir;
		}
	}

	@Override
	public String toString() {
		return id;
	}
}